﻿ /*************************************************************************
File:   as/playback.as
Author: Duykham
UpDate: 19/05/2007
TODO:
/*************************************************************************/
echo ("#included: playback.as");
///////////////////////////////////////////////////////////////////////////
//included files
///////////////////////////////////////////////////////////////////////////
//khai bao bien
//Tao mot ket noitoi FMS
myConnection3 = new NetConnection ();
//myConnection3.connect (_root.CFG.fmsApp);
myConnection3.connect (_root.CFG.fmsApp);
in_ns = new NetStream (myConnection3);
var camerasList_lst : mx.controls.List;
var folder_lst : mx.controls.List;
//var output_txt : mx.controls.TextArea;
var loadFolder = new LoadVars ();
var loadFiles = new LoadVars ();
// tao mot doi tuong LoadVar moi.
var folderList = new Array ();
var filesList = new Array ();
var changeCam = new Object ();
var changeFolder = new Object ();
var changeDate = new Object ();
var scrollMonth = new Object ();
//var mySound = new Sound;
var content_txt : String = new String ();
var thisDate = clock.strDate;
var dateFormatedArray : Array = new Array ();
//Log
var rsoMessages ;
var rsoDate;
//Play button
var labelButton_txt;
var text1_txt;
var text2_txt;
///////////////////////////////////////////////////////////////////////////
//Initialize
//rsoMessages
rsoMessages = SharedObject.getRemote ("log/" + clock.strDate, myConnection3.uri, true);
rsoMessages.connect (myConnection3);
rsoDate = clock.strDate;
labelButton_txt = "Play";
//TODO: Load Folder when start
loadFolder.load (_root.CFG.webAdd + "list.php?dir=" + _root.CFG.fmsDir + "&value=streams\\_definst_\\archive");
trace (_root.CFG.webAdd + "list.php?dir=" + _root.CFG.fmsDir + "&value=streams\\_definst_\\archive");
///////////////////////////////////////////////////////////////////////////
//Functions
//TODO: Load Folder
loadFolder.onLoad = function (success)
{
	if (success)
	{
		var newItem = new Object ();
		var nameStr = new String ();
		var typeStr = new String ();
		//var folderList = new Array ();
		for (var role in this)
		//trong khi dang con duyet den phan tu role trong mang loadFolder
		if (role != "onLoad" && this [role] != "undefined")
		{
			nameStr = this [role];
			//Lay ra ten cua loai file VD: .flv
			//typeStr = nameStr.substr (nameStr.indexOf (".") , 4);
			//loai bo duoi .flv trong ten file
			//nameStr = nameStr.substr (0, nameStr.indexOf ("."));
			//text = text + nameStr + typeStr+ newline;
			//if (typeStr != ".flv" && typeStr != ".fso")
			if (nameStr.length == 10)
			{
				newItem.label = nameStr;
				newItem.data = nameStr;
				//trace("newItem.label: "+ newItem.label);
				folderList.push ( 
				{
					label : newItem.label, data : newItem.data
				});
				//trace("folderList: "+folderList);
			} 
			else
			{
				//do nothing
			}
		} 
		else
		{
			//do nothing
		}
		formatDateArray (folderList); 
		configDC ();
		//De List box hien thi ngay hien tai
		for (var i : Number = 0; i < folder_lst.length; i ++)
		//for (var i:Number = 0; i < 7; i++)
		{
			if (folder_lst.getItemAt (i).label == clock.strDate)
			trace ("clock.strDate: " + clock.strDate);
			//if (folder_lst.getItemAt(i) == "14-06-2006")
			{
				folder_lst.selectedIndex = i;
				break;
			}
		}
		loadFiles.load (_root.CFG.webAdd + "list.php?dir=" + _root.CFG.fmsDir + "&value=streams\\_definst_\\archive\\" + folder_lst.value);
	}
};
//TODO: Load Files
loadFiles.onLoad = function (success)
{
	//var i=0;
	//filesList = null;
	_root.filesList = new Array ();
	if (success)
	{
		var newItem = new Object ();
		var nameStr = new String ();
		var typeStr = new String ();
		for (var role in this)
		//trong khi dang con duyet den phan tu role trong mang
		if (role != "onLoad" && this [role] != "undefined")
		{
			nameStr = this [role];
			//Lay ra ten cua loai file VD: .flv
			typeStr = nameStr.substr (nameStr.lastIndexOf (".") , 4);
			//loai bo duoi .flv trong ten file
			nameStr = nameStr.substr (0, nameStr.lastIndexOf ("."));
			//text = text + nameStr + typeStr+ newline;
			//if (typeStr != ".flv" && typeStr != ".fso")
			if (typeStr == ".flv")
			{
				newItem.label = nameStr;
				newItem.data = nameStr;
				//trace("newItem.label: "+ newItem.label);
				_root.filesList.push ( 
				{
					label : newItem.label, data : newItem.data
				});
				//trace("filesList: "+filesList);
				
			} 
			else
			{
				//do nothing
				
			}
		} 
		else
		{
			//do nothing
			
		}
		camerasList_lst.dataProvider = _root.filesList;
		//_root.filesList.splice (0, _root.filesList.length);
		
	}
};
//TODO: change folder list
changeFolder.change = function (event)
{
	trace ("folder_lst.value: " + folder_lst.value);
	thisDate = folder_lst.value;
	loadFiles.load (_root.CFG.webAdd + "list.php?dir=" + _root.CFG.fmsDir + "&value=streams\\_definst_\\archive\\" + folder_lst.value);
	trace ("thisDate: " + thisDate);
	//clear Files list
	/*
	var my_array = new Array();
	_root.camerasList_lst.dataProvider = my_array;
	*/
	//loadFiles.load (null);
	//for LOG
	rsoDate = event.target.value;
	rsoMessages = SharedObject.getRemote ("log/" + rsoDate, myConnection3.uri, true);
	rsoMessages.connect (myConnection3);
	//Press View Log
	view_btn.onRelease ();
};
folder_lst.addEventListener ("change", changeFolder);
/*
//TODO: change Date
changeDate.change = function (event)
{
var newstr = formatDate(string(calendar_fc.selectedDate));
loadFiles.load (_root.CFG.webAdd+"list.php?dir="+_root.CFG.fmsDir+"&value=streams\\_definst_\\archive\\" + newstr);
thisDate = newstr;
trace ("thisDate: "+thisDate);
};
calendar_fc.addEventListener ("change", changeDate);

//TODO: listener Scroll Month

scrollMonth.scroll = function (event)
{
configDC();
for (i=1; i<31; i++)
{
var flag:Boolean = false;
var datestring = (i<10)?("0"+i): i;
var monthstring = (calendar_fc.displayedMonth<9)? "0"+(calendar_fc.displayedMonth+1) : (calendar_fc.displayedMonth+1);
var yearstring = calendar_fc.displayedYear;
var fullDateString = datestring+"-"+monthstring+"-"+yearstring;
trace ("fullDateString: "+fullDateString);
for (j=0; j<folder_lst.length; j++)
{
if(datestring == folder_lst[j].label)
{
flag = true;
trace("flag =true, fullDateString: "+fullDateString);
break;
};
};
if (!flag)
{
calendar_fc.disabledRanges =[new Date(calendar_fc.displayedYear, calendar_fc.displayedMonth, i)];
trace("disabledRanges : "+ calendar_fc.displayedYear+ calendar_fc.displayedMonth+ i );
};
};
};
calendar_fc.addEventListener("scroll", scrollMonth);
*/
// TODO: formatDateArray
function formatDateArray (dateArray : Array)
{
	for (i = 0; i < dateArray.length; i ++)
	{
		var newArr = dateArray [i].label.split ("-");
		//trace ("newArr: "+newArr[0]);
		_root.dateFormatedArray.push ( 
		{
			year : newArr [2] , month : newArr [1] , date : newArr [0] , string : newArr [2] + newArr [1] + newArr [0]
		});
	}
	_root.dateFormatedArray.sortOn ("string");
	_root.dateFormatedArray.reverse ();
	newFolderList = new Array ();
	for (i = 0; i < dateFormatedArray.length; i ++)
	{
		trace ("dateFormatedArray: " + dateFormatedArray [i].year + dateFormatedArray [i].month + dateFormatedArray [i].date);
		newFolderList.push ( 
		{
			label : dateFormatedArray [i].date + "-" + dateFormatedArray [i].month + "-" + dateFormatedArray [i].year, data : dateFormatedArray [i].date + "-" + dateFormatedArray [i].month + "-" + dateFormatedArray [i].year
		});
	};
	folder_lst.dataProvider = newFolderList;
};
/*
//TODO: Format date of DateChooser Component
function formatDate (stringdate:String) :String
{
//
trace("stringdate: "+stringdate);
my_str = stringdate;
var DCarray:Array = my_str.split(" ");
trace ("DCarray[1]: "+DCarray[1]);
var DCmonth = DCarray[1];
var DCdate:String = (DCarray[2] <10)? ("0"+DCarray[2]) : DCarray[2];
var DCyear = DCarray[5];
var DCmonthNum;
var monthArray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
for (var i=0; i<12; i++)
{
if (monthArray[i] == DCmonth)
{
DCmonthNum = (i+1 <10)? "0"+(i+1) : i+1;
trace ("monthArray["+i+"]: "+monthArray[i]);
trace("DCmonth: "+DCmonth);
};
};
return (DCdate+"-"+DCmonthNum+"-"+DCyear);
};

//config Date Chooser Component
function configDC ()
{
trace ("configDC()");
endDate = Number(dateFormatedArray[dateFormatedArray.length-1].date)-1;//boi vi khong disable endDate
endMonth = dateFormatedArray[dateFormatedArray.length-1].month-1;
endYear = dateFormatedArray[dateFormatedArray.length-1].year;
startDate = Number(dateFormatedArray[0].date)+1;//boi vi khong disable startDate
startMonth = dateFormatedArray[0].month-1;
startYear = dateFormatedArray[0].year;
_root.calendar_fc.disabledRanges = [{rangeEnd: new Date(endYear, endMonth, endDate)}];
_root.calendar_fc.disabledRanges = [{rangeStart: new Date(startYear, startMonth, startDate)}];
trace ("startDate: "+startDate+", startMonth: "+startMonth+", startYear: "+startYear+"\nendDate: "+endDate+", endMonth: "+endMonth+", endYear: "+endYear);
};
*/
//TODO: Play button release
play_btn.onRelease = function ()
{
	if (camerasList_lst.value != undefined && labelButton_txt == "Play")
	{
		in_ns.play ("archive/" + thisDate + "/" + camerasList_lst.value);
		my_video.attachVideo (in_ns);
		//_root.mySound.setVolume (100);
		text1_txt = "Now playing :";
		text2_txt = camerasList_lst.value;
		//play_btn.label = "Stop";
		labelButton_txt = "Stop";
		camerasList_lst.enabled = false;
		folder_lst.enabled = false;
		exit_btn.enabled = false;
		viewLog ();
	} 
	else
	{
		if (camerasList_lst.value == undefined)
		{
			toolTip.show ("Please select a video!", 100, 40, 86, 280);
		};
		my_video.attachVideo (null);
		in_ns.close ();
		my_video.clear ();
		//play_btn.label = "Play";
		labelButton_txt = "Play";
		camerasList_lst.enabled = true;
		folder_lst.enabled = true;
		exit_btn.enabled = true;
		text1_txt = "Select a Video!";
		text2_txt = "";
	}
};
//TODO: Play button rollout
play_btn.onRollOut = function ()
{
	toolTip.hide ();
}
/*
//TODO: trace button
trace_btn.onRelease = function ()
{
trace ("calendar_fc.selectedDate: " + calendar_fc.selectedDate);
var newstr = formatDate (string (calendar_fc.selectedDate));
trace ("newstr: " + newstr)
};
*/
//TODO: View log
viewLog = function ()
{
	var mList = rsoMessages.data.logs;
	var i = 0;
	report_txt.text = "";
	report_txt.text = "LOG : " + folder_lst.value + newline + "--------------------------------------------------------------------------" + newline;
	while (mList [i] != undefined)
	{
		//while (i=0; i < mList.length(); i++){
		report_txt.text = report_txt.text + newline + "Time: " + mList [i].label + newline + "Motion level:  " + mList [i].data + newline;
		i ++;
	}
};
//TODO: Del Folder button release
delfolder_btn.onRelease = function ()
{
	if (folder_lst.value != undefined)
	{
		alertMsg.title = "Delete Date";
		alertMsg.alertLogo = "Shape 301";
		alertMsg.buttonLabel = ["Delete", "Cancel"];
		alertMsg.buttonFunction = ["deleteFolder", "cancelFunction"];
		alertMsg.text = "All videos on the date " + folder_lst.value + " will be deleted! Are you sure?";
		alertMsg.alert ();
	}
};
/*
//TODO: Tool Tip for Del Folder button
delfolder_btn.onRollOver = function()
{
toolTip.show ("Delete", 42, 23, _xmouse, _ymouse);
};
delfolder_btn.onRollOut = function()
{
toolTip.hide();
};
*/
delfile_btn.onRelease = function ()
{
	//TODO: Del File button release
	if (camerasList_lst.value != undefined)
	{
		alertMsg.title = "Delete video";
		alertMsg.alertLogo = "Shape 301";
		alertMsg.buttonLabel = ["Delete", "Cancel"];
		alertMsg.buttonFunction = ["deleteFile", "cancelFunction"];
		alertMsg.text = "The video " + camerasList_lst.value + " will be deleted! Are you sure?";
		alertMsg.alert ();
	}
};
/*
//TODO: Tool Tip for Del File button
delfile_btn.onRollOver = function()
{
toolTip.show ("Delete", 42, 23, _xmouse, _ymouse);
};
delfile_btn.onRollOut = function()
{
toolTip.hide();
};
*/
//TODO: delete folder function
deleteFolder = function ()
{
	trace (folder_lst.value + " was deleted successfully!");
};
//TODO: delete file function
deleteFile = function ()
{
	var newArr = new Array ();
	trace (camerasList_lst.value + " was deleted successfully!");
	//newArr.splice (0, 1);
	_root.camerasList_lst.dataProvider = newArr;
};
//TODO: cancel function
cancelFunction = function ()
{
	trace ("Canceled!");
};
//Initialize
text1_txt = "Select a Video!";
text2_txt = "";
camerasList_lst.setStyle ("themeColor", "haloBlue");
camerasList_lst.rowHeight = 30;
folder_lst.setStyle ("themeColor", "haloBlue");
folder_lst.rowHeight = 30;